package com.ihome.android.market2.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.RelativeLayout;

import com.ihome.android.academy.AlbumAppActivity;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.image.CubeImageView;
import com.ihome.android.image.ImageLoader;
import com.ihome.android.academy.R;
import com.ihome.android.market2.frag.AlbumListFragment.AlbumItemAdapter;
import com.ihome.android.market2.info.TemplateContentInfo;
import com.ihome.android.util.log.LogUtil;

@SuppressLint("NewApi")
public class AlbumListItem implements OnClickListener {
	private final String		TAG	= AlbumListItem.class.getSimpleName();
	private RelativeLayout		mRelativeLayout;

	private RelativeLayout		fl_content;
//	private RelativeLayout			frameRelativeLayout;
	private CubeImageView		item_icon;
//	private CubeImageView			item_frame;
	private Context				context;
	private ImageLoader			imageLoader;

	private AlbumItemAdapter	adapter;
	private TemplateContentInfo	appInfo;
	public int					position;

	public AlbumListItem(Context context, ImageLoader imageLoader) {
		this.context = context;
		this.imageLoader = imageLoader;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.album_list_item, null);
		fl_content = (RelativeLayout) mRelativeLayout.findViewById(R.id.fl_content);
//		frameRelativeLayout = (RelativeLayout) mRelativeLayout.findViewById(R.id.fl);

		item_icon = (CubeImageView) mRelativeLayout.findViewById(R.id.item_icon);
//		item_frame = (CubeImageView) mRelativeLayout.findViewById(R.id.itemframe);

		fl_content.setOnClickListener(this);
		fl_content.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				LogUtil.w(TAG, "onFocusChange() --> fl_content hasFocus = " + hasFocus + "  position = " + position);
				if (hasFocus) {
					adapter.setFocusedPosition(position);
					hasFocus();
				}
				else {
					recovered();
				}
				checkMainViewFocus();
			}
		});
	}

	/**
	 * 初始化View，根据AppInfo填充View
	 * 
	 * @param adapter
	 * @param appInfo
	 * @param position
	 */
	public void initView(AlbumItemAdapter adapter, TemplateContentInfo appInfo, int position) {
		this.adapter = adapter;
		this.appInfo = appInfo;
		this.position = position;

		if (appInfo != null) {
			//			item_appname.setText(appInfo.getName());

			if (!TextUtils.isEmpty(appInfo.getPosterUrl())) {
				item_icon.loadImage(imageLoader, appInfo.getPosterUrl());
			}
			else {
				item_icon.setImageResource(R.drawable.icon_default);
			}
		}

	}

	private void checkMainViewFocus() {
		if (!mRelativeLayout.hasFocus()) {
			recovered();
		}
	}

	public void recovered() {
		LogUtil.w(TAG, "recovered() -->  position = " + position);
		ScaleItemAnim anim = new ScaleItemAnim();
		anim.setParams(1.1F, 1.0F, 1.1F, 1.0F, 0L);
		fl_content.startAnimation(anim.getAnimation());
//		frameRelativeLayout.setVisibility(View.INVISIBLE);
//		item_frame.setVisibility(View.INVISIBLE);
	}

	public void hasFocus() {
		LogUtil.i(TAG, "hasFocus() -->  position = " + position);
//		mRelativeLayout.bringToFront();
		
		ScaleItemAnim anim = new ScaleItemAnim();
		anim.setParams(1.0F, 1.1F, 1.0F, 1.1F, 100L);
		fl_content.startAnimation(anim.getAnimation());
//		frameRelativeLayout.setVisibility(View.VISIBLE);
//		item_frame.setVisibility(View.VISIBLE);
	}

	public void requestFocus() {
		adapter.setFocusedPosition(position);
		hasFocus();
		fl_content.requestFocus();
	}

	public void setClickListener(OnClickListener onClickListener) {
		mRelativeLayout.setOnClickListener(onClickListener);
	}

	public View getView() {
		return mRelativeLayout;
	}

	public void setLayoutParams(RelativeLayout.LayoutParams params) {
		mRelativeLayout.setLayoutParams(params);
	}

	public void setTag(Object tag) {
		mRelativeLayout.setTag(tag);
	}

	public void setOnKeyListener(OnKeyListener onkeylistener) {
		mRelativeLayout.setOnKeyListener(onkeylistener);
		fl_content.setOnKeyListener(onkeylistener);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.fl_content:
			LogUtil.w(TAG, "onClick() --> fl_content  position = " + position);
			break;
		default:
			break;
		}
	}

}
